var app = getApp();
var config = require('../../../config.js');
Page({
  data: {
    ProductKeys:[],
    isActive: false,
    
    searchProdut:'',
    page:1,
    pageSize:10,
    sortType:0,
    ProductList: [],
    hasMoreData: true,
    imgPath: config.imgPath
  },
  onShareAppMessage: function () {

  },
  zonghe(){
    this.setData({
      sortType : 0,
      page:1
    })
    this.search()
  },
  dealCount(){
    this.setData({
      sortType : 1,
      page:1
    })
    this.search()
  },
  dealRate(){
    this.setData({
      sortType : 2,
      page:1
    })
    this.search()
  },
  searchProdutInput(e){
    this.setData({
      searchProdut : e.detail.value
    })
  },
  select(e){
    let index = e.currentTarget.dataset.index
    var list = this.data.ProductKeys
    this.setData({
      ['ProductKeys[' + index + '].active']: !list[index].active,
    
    })
    //console.log(index)
  },
  loadKeys(){
    let self = this
    app.request({
      url: 'api/wechat/getProductKeys',
      method: "GET",
      success: function (res) {
        var temp = res.data.Data
        for(var i = 0;i<temp.length;i++){
          temp[i].active = false
        }
        self.setData({
          ProductKeys: temp
        })
      }
    })
  },
  search(){
    var keys = new Array();
    var list = this.data.ProductKeys
    for(var i = 0;i<list.length;i++){
      if (list[i].active) {
        keys.push(list[i].name)
      }
    }
    let self = this
    app.request({
      url: 'api/wechat/GetPageOfProductByWechat',
      method: "POST",
      data: {
        sortType:self.data.sortType,
        productName:self.data.searchProdut,
        productKeys:keys,
        page: self.data.page,
        pageSize: self.data.pageSize
      },
      success: function (res) {
        console.log(res)
        if (res.data != null && res.data.Data.length > 0) {
          var listTemp = self.data.ProductList

          if (self.data.page == 1) {
            listTemp = []
          }
          var list = res.data.Data
          if (list.length < self.data.pageSize) {
            self.setData({
              ProductList: listTemp.concat(list),
              hasMoreData: false
            })
          } else {
            self.setData({
              ProductList: listTemp.concat(list),
              hasMoreData: true,
              page: self.data.page + 1
            })
          }
        }else{
          self.setData({
            ProductList: [],
            hasMoreData: false
          })
        }
      }
    })
  },
  cancelFilter(){
    this.setData({
      isActive: false
    })
    var list = this.data.ProductKeys
    for(var i = 0;i<list.length;i++){
      list[i].active = false
    }
    this.setData({
      ProductKeys:list
    })
  },
  okFilter(){
    this.setData({
      isActive: false,
      page:1
    })
    this.search()
  },
  changeStatus(e) {
    let status = e.currentTarget.dataset.status;
    this.setData({
      isActive: !this.data.isActive,
    })
  },
  onShow: function () {
    this.loadKeys()
    this.setData({
      page:1
    })
    this.search()
  },
  //下拉
  onPullDownRefresh: function () {
    this.setData({
      page: 1
    })
    this.search()
    wx.stopPullDownRefresh();
  },
  //上拉
  onReachBottom: function () {
    if (this.data.hasMoreData) {
      this.search()
    } else {
      wx.showToast({
        title: '没有更多了',
        icon: 'none',
      })
    }
  },
})